#!/usr/bin/perl -w

use strict;
use threads;  
use Net::Ping;
use Data::Dumper;
use threads::shared;

my @domain   =   ("www.baidu.com",   "www.google.com",   "www.qq.com",   "www.taobao.com","www.163.com","www.126.com");   
#my @thread;
my $t:shared;
my $max_thread=3;
my $child_num=0;

while(1)
{
	if ($child_num < $max_thread)
	{
	if ( $#domain >= 0 ){
	my $d=shift @domain; 
	my $thread=threads->new(\&checkalive,$d);
	$child_num++;
	}
	}
	for my $t(threads->list(threads::joinable))
	{
	$t->join();
	$child_num--;
	}
	if ("tasks done" && $child_num==0)
	{
	last;
	}
}
print $t,"\n";
sub checkalive()
{
my $host=shift;
my $p=Net::Ping->new("icmp");
print "$host reachable\n" if $p->ping($host,5);
sleep 5;
$t++;
}

